$NG.AllReady(function (page, { useValuesChange, useDataIndexChange, useUpdateRows, useUpdateRow }) {
    const mstform = $NG.getCmpApi("p_form_djjg_m");
    const dgrid1 = $NG.getCmpApi("p_form_djjg_d");

    const storedData = localStorage.getItem("selectedUSourceData");
	console.log(storedData);
	
	if (storedData) {
		const parsedData = JSON.parse(storedData);
		// 获取行数
		console.log(parsedData);

		const gridCount = parsedData.length;
  
		let gridArr = []; // 初始化数组
  
		// 将 execServer 封装为 Promise
		function fetchData(item) {
  
		  return new Promise((resolve, reject) => {
			$NG.execServer("getDjxqcByPhid", { phid: item.u_source_id, resource_table: `'${item.u_source_table}'` }, function (res) {
			  try {
				console.log("进入功能扩展getDjxqcByPhid1");
				if (res.count > 0) {
				  const data = JSON.parse(res.data)[0].extendObjects;
				  console.log(data);
				  let gridObj = {
					u_source: data.djly,
					u_material_num: data.item_phid,
					u_material_num_EXName: data.item_no,
					u_material_name: data.item_name,
					// u_item_no: data.item_phid,
					// u_item_no_EXName: data.item_no,
					u_item_name: data.item_name,
					u_unit: data.unit_phid,
					u_unit_EXName: data.unit_name,
					u_spec: data.spec,
					u_sj_spec: data.spec,
					u_wlfl: data.resbs_phid,
					u_wlfl_EXName: data.resbs_name,
					u_ncg_qty: data.u_qty,
					u_sqyjjg: data.u_vat_prc,
					u_pc_no:data.u_sj_no,
					u_sj_name:data.u_sj_name,
					u_zb_supply:data.u_zb_supply,
					u_zb_qty:data.u_qty,
					u_zb_prc:data.u_zb_prc,
					u_zb_prc_vat:data.u_zb_prc_vat,
					u_zb_rate:data.u_zb_rate,
				  };
  
				  resolve(gridObj);
				} else {
				  console.log("功能扩展暂无数据");
				  resolve(null);
				}
			  } 
			  
			  catch (error) {
				console.error("功能扩展报错:", error);
				reject(error);
			  }
			});
  
		  });
		}
  
		// 执行所有异步请求
		async function fetchAllData() {
		  try {
			let results = await Promise.all(parsedData.map(fetchData));
			gridArr = results.filter(Boolean); // 过滤掉 null
			// 调用新增表体行数据的函数
			if (gridArr.length > 0) {
			  dgrid1.addRows(gridArr);
			} else {
			  console.warn("未能添加任何数据");
			}
			// 清除 LocalStorage 数据
			localStorage.removeItem("selectedUSourceData");
			console.log("LocalStorage 已清除");
		  } catch (error) {
			console.error("获取数据出错:", error);
		  }
		}
		fetchAllData(); // 调用异步方法
	  } else {
		console.log("LocalStorage 中没有数据");
	  }
    ////////////////////////////////////////////////////////////--useUpdateRows--////////////////////////////////////////////////////////////
    useUpdateRows(({ args }) => {
      console.log(`表体监听`, args[0]);
      let rows  = args[0]
		// 遍历数组中的每个对象
        rows.forEach((item) => {
			//默认剩余量
            let sjno = item.u_pc_no;
            console.log(sjno);
            item.u_zb_prc = (Number(item.u_zb_prc_vat) || 0) /  (1 + (Number(item.u_zb_rate) || 0));
            item.u_zbwsje = (Number(item.u_zb_prc) || 0)*(Number(item.u_zb_qty) || 0);
            item.u_zbhsje = (Number(item.u_zb_prc_vat) || 0) * (Number(item.u_zb_qty) || 0);
			if (item.u_pc_no) {
				$NG.execServer("getPcBySj", { sjno: `'${sjno}'`}, function (res) {
					const datas = JSON.parse(res.data)[0].extendObjects;
					console.log(datas, "xxxxxxxx");
					item.phid_pc = datas.phid_pc;
					item.phid_pc_EXName = datas.pc_name; //单位
					// item.u_item_sub = datas.res_bs_phid; //单位
					// item.u_item_sub_EXName = datas.res_bs_name; //单位
					dgrid1.refreshView();
				});
			}
		});
	
      dgrid1.refreshView();
    }, "p_form_djjg_d");

  });
  